package com.jilian.mydemo.modul.main.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jilian.mydemo.R;
import com.jilian.mydemo.common.dto.MsgDto;
import com.jilian.mydemo.listener.CustomItemClickListener;

import java.util.List;


public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
    private Activity mContext;
    private List<MsgDto> datas;
    private CustomItemClickListener listener;

    public MsgAdapter(Activity context, List<MsgDto> datas, CustomItemClickListener listener) {
        mContext = context;
        this.datas = datas;
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_msg, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.tvContent.setText(Html.fromHtml(datas.get(position).getContent()));
        holder.tvTitle.setText(datas.get(position).getTitle());
        holder.tvDate.setText(datas.get(position).getCreateTime());
        if(datas.get(position).getStatus()==0){
            holder.ivPoints.setVisibility(View.VISIBLE);
        }
        else{
            holder.ivPoints.setVisibility(View.GONE);
        }


    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvDate;
        private TextView tvTitle;
        private TextView tvContent;
        private ImageView ivPoints;





        public ViewHolder(final View itemView, final CustomItemClickListener listener) {
            super(itemView);
            tvDate = (TextView)itemView. findViewById(R.id.tv_date);
            tvTitle = (TextView)itemView. findViewById(R.id.tv_title);
            tvContent = (TextView) itemView.findViewById(R.id.tv_content);
            ivPoints = (ImageView)itemView. findViewById(R.id.iv_points);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onItemClick(itemView,getAdapterPosition());
                }
            });
        }

    }
}
